package com.iespring.service.admin.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.goods.bo.*;
import com.iespring.bean.admin.goods.po.*;
import com.iespring.bean.admin.goods.vo.*;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.*;
import com.iespring.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author KeFei
 * @description: GoodsServiceImpl
 * @date 2022/12/29 9:51
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    MallGoodsMapper goodsMapper;

    @Resource
    MallBrandMapper brandMapper;

    @Resource
    MallCategoryMapper categoryMapper;

    @Resource
    MallGoodsAttributesMapper attributesMapper;

    @Resource
    MallGoodsProductMapper productMapper;

    @Resource
    MallGoodsSpecificationsMapper specificationsMapper;

    @Resource
    MallCommentMapper commentMapper;

    /**
     * 显示商品列表
     *
     * @param basePageInfo
     * @return
     */
    @Override
    public List<com.iespring.bean.admin.goods.po.MallGoods> showGoodsList(BasePageInfo basePageInfo) {

        List<MallGoods> goodsList = goodsMapper.showGoodsList(basePageInfo);

        return goodsList;
    }

    /**
     * 获取商品上架前的所需信息
     *
     * @return
     */
    @Override
    public GoodsInfoVo getGoodsInfo() {

        // 查询商品的品牌信息
        List<GoodsBrandInfoVo> brandList = brandMapper.getGoodsBrandInfo();

        // 查询商品的类目信息
        List<MallCategory> mallCategoryList = categoryMapper.getGoodsCategoryInfo();

        List<GoodsCategoryInfoVo> categoryList = new ArrayList<>();
        for (MallCategory mallCategory1 : mallCategoryList) {
            // 找到商品的父级类目
            if (mallCategory1.getPid() == 0) {
                // 获取商品子类目信息的对象数组
                List<GoodsCategoryChildrenInfoVo> categoryChildrenList = new ArrayList<>();
                for (MallCategory mallCategory2 : mallCategoryList) {
                    if (mallCategory2.getPid().equals(mallCategory1.getId())) {
                        // 说明mallCategory2是mallCategory1的子级类目
                        // 将mallCategory2添加到子级类目数组categoryChildrenList中
                        categoryChildrenList.
                                add(new GoodsCategoryChildrenInfoVo(mallCategory2.getName(), mallCategory2.getId()));
                    }
                }
                // 获取商品父类目信息的对象数组
                categoryList.
                        add(new GoodsCategoryInfoVo(categoryChildrenList, mallCategory1.getName(), mallCategory1.getId()));
            }
        }
        // 封装商品的品牌商信息和类目信息并返回
        return new GoodsInfoVo(brandList, categoryList);
    }

    /**
     * 上架商品
     *
     * @param addGoodsBo
     */
    @Override
    public void addGoods(AddGoodsBo addGoodsBo) {

        // 商品的主要信息
        GoodsBo goods = addGoodsBo.getGoods();
        // 商品的参数信息
        List<GoodsAttributesBo> attributes = addGoodsBo.getAttributes();
        // 商品的规格信息
        List<GoodsProductsBo> products = addGoodsBo.getProducts();
        List<GoodsSpecificationsBo> specifications = addGoodsBo.getSpecifications();

        Date date = new Date();
        // 获取当前商品各规格中的最低售价
        List<GoodsProductsBo> productsList = addGoodsBo.getProducts();
        BigDecimal retailPrice = new BigDecimal(999999999);
        for (GoodsProductsBo goodsProducts : productsList) {
            if (retailPrice.compareTo(goodsProducts.getPrice()) == 1) {
                retailPrice = goodsProducts.getPrice();
            }
        }

        // 插入商品信息
        MallGoods goodsInfo = new MallGoods
                (null, goods.getGoodsSn(), goods.getName(), goods.getCategoryId(), goods.getBrandId(), goods.getGallery(), goods.getKeywords(), goods.getBrief(), goods.getIsOnSale(), null, goods.getPicUrl(), null, goods.getIsNew(), goods.getIsHot(), goods.getUnit(), goods.getCounterPrice(), retailPrice, goods.getDetail(), date, date, false);
        goodsMapper.addGoods(goodsInfo);
        // 修改goodsSn的值为商品id的值
        Integer goodsId = goodsInfo.getId();
        goodsMapper.updateGoodsSn(goodsId);
        // 插入商品参数信息
        for (GoodsAttributesBo goodsAttribute : attributes) {
            attributesMapper.addGoodsAttributes
                    (new MallGoodsAttribute(null, goodsId, goodsAttribute.getAttribute(), goodsAttribute.getValue(), date, date, false));
        }
        // 插入商品规格信息
        for (GoodsProductsBo goodsProduct : products) {
            String[] goodsProductSpecifications = goodsProduct.getSpecifications();
            String s = Arrays.toString(goodsProductSpecifications);
            productMapper.addGoodsProducts
                    (new MallGoodsProduct(null, goodsId, s, goodsProduct.getPrice(), Integer.parseInt(goodsProduct.getNumber()), goodsProduct.getUrl(), date, date, false));
        }
        for (GoodsSpecificationsBo goodsSpecification : specifications) {
            specificationsMapper.addGoodsSpecifications
                    (new MallGoodsSpecification(null, goodsId, goodsSpecification.getSpecification(), goodsSpecification.getValue(), goodsSpecification.getPicUrl(), date, date, false));
        }
    }

    /**
     * 获取要编辑商品的信息
     *
     * @param id
     * @return
     */
    @Override
    public UpdateGoodsInfoVo getUpdateGoodsInfo(Integer id) {

        // 获取商品的主要信息
        UpdateGoodsGoodsInfoVo goods = goodsMapper.getUpdateGoodsGoodsInfo(id);

        // 获取商品的父级类目信息
        Integer fatherCategoryId = goods.getCategoryId();
        // 获取商品的子级类目信息
        Integer sonCategoryId = categoryMapper.getGoodsSonCategoryId(fatherCategoryId);
        // 封装
        List<Integer> categoryIds = new ArrayList<>();
        categoryIds.add(fatherCategoryId);
        categoryIds.add(sonCategoryId);

        // 获取商品的参数信息
        List<UpdateGoodsAttributesInfoVo> attributes = attributesMapper.getUpdateGoodsAttributesInfo(id);
        // 获取商品的详细规格信息
        List<UpdateGoodsProductsInfoVo> products = productMapper.getUpdateGoodsProductsInfo(id);
        // 获取商品的规格信息
        List<UpdateGoodsSpecificationsInfoVo> specifications = specificationsMapper.getUpdateGoodsSpecificationsInfo(id);

        // 封装所有Vo
        UpdateGoodsInfoVo updateGoodsInfoVo = new UpdateGoodsInfoVo(attributes, categoryIds, goods, products, specifications);
        return updateGoodsInfoVo;
    }

    /**
     * 更新编辑后的商品
     *
     * @param updateGoodsInfoBo
     */
    @Override
    public void updateGoodsInfo(UpdateGoodsInfoBo updateGoodsInfoBo) {

        // 更新的商品主要信息
        UpdateGoodsGoodsInfoBo goods = updateGoodsInfoBo.getGoods();
        // 更新的商品参数的信息
        List<UpdateGoodsAttributesInfoBo> attributes = updateGoodsInfoBo.getAttributes();
        // 更新的商品的详细规格信息
        List<UpdateGoodsProductsInfoBo> products = updateGoodsInfoBo.getProducts();
        // 更新的商品的规格信息
        List<UpdateGoodsSpecificationsInfoBo> specifications = updateGoodsInfoBo.getSpecifications();
        // 获取修改商品的商品id
        Integer goodsId = goods.getId();

        // 修改商品的主要信息
        goodsMapper.updateGoodsInfo(goods);
        // 修改商品的参数信息
        for (UpdateGoodsAttributesInfoBo attribute : attributes) {
            // 判断是修改已有的商品参数还是添加新的商品参数
            if (attribute.getId() != null) {
                // 修改已有的商品参数
                attributesMapper.updateGoodsAttributesInfo(attribute);
            } else {
                // 新添加的商品参数
                attributesMapper.updateNewGoodsAttributesInfo
                        (new MallGoodsAttribute(null, goodsId, attribute.getAttribute(), attribute.getValue(), new Date(), new Date(), false));
            }
        }
        // 修改商品的详细规格信息
        for (UpdateGoodsProductsInfoBo product : products) {
            productMapper.updateGoodsProductInfo(product);
        }
        // 修改商品的规格信息
        for (UpdateGoodsSpecificationsInfoBo specification : specifications) {
            specificationsMapper.updateGoodsSpecificationInfo(specification);
        }
    }

    /**
     * 删除商品
     *
     * @param deleteGoodsBo
     */
    @Override
    public void deleteGoods(DeleteGoodsBo deleteGoodsBo) {

        // 逻辑删除
        deleteGoodsBo.setDeleted(true);

        goodsMapper.deleteGoods(deleteGoodsBo);

    }

    /**
     * 显示商品评论列表
     *
     * @param basePageInfo
     * @return
     */
    @Override
    public CommonData showCommentList(BasePageInfo basePageInfo) {

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        List<MallComment> commentList = commentMapper.showCommentList(basePageInfo);

        PageInfo<MallComment> pageInfo = new PageInfo<>(commentList);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    /**
     * 回复评论
     *
     * @param map
     * @return
     */
    @Override
    public int replyComment(Map map) {

        Integer commentId = (Integer) map.get("commentId");
        String content = (String) map.get("content");

        // 判断评论是否已回复
        String replyComment = commentMapper.isReply(commentId);
        if (!StrUtil.isEmpty(replyComment)) {
            // 评论已回复
            return 1;
        }
        // 评论未回复
        commentMapper.reply(commentId, content);
        return 0;
    }

    /**
     * 删除评论
     *
     * @param deleteCommentBo
     */
    @Override
    public void deleteComment(DeleteCommentBo deleteCommentBo) {
        Integer commentId = deleteCommentBo.getId();

        commentMapper.deleteComment(commentId);
    }
}
